﻿using System;
using System.Collections.Generic;
using System.Drawing;
using CanvasControl.Model;
using CanvasControl.View;
using GraphDrawing.Graph.Model;

namespace GraphDrawing.Graph.View
{
    public abstract class BlockView
    {
        readonly Dictionary<int, SlotView> viewBindings = new Dictionary<int, SlotView>();

        public Appearance Default { get; set; }
        public Appearance Selected { get; set; }

        protected BlockView()
        {
            Default = new Appearance();
            Selected = new Appearance();
        }

        protected void BindSlotView(int index, SlotView view)
        {
            viewBindings[index] = view;
        }

        protected SlotView GetSlotView(int index)
        {
            SlotView v;
            if (!viewBindings.TryGetValue(index, out v))
                throw new InvalidOperationException();
            return v;
        }

        public abstract void Draw(DrawingContext dc, ViewPortModel viewPort, BlockModel block);
    }

    public class RectangleBlockView : BlockView
    {
        public override void Draw(DrawingContext dc, ViewPortModel viewPort, BlockModel block)
        {
            var appearance = block.IsSelected ? Selected : Default;
//            var shape = (RectangleShape) block.Shape;
//            var clientRect = viewPort.GlobalToClient(new Rectangle(block.position, new Size(shape.width, shape.height)));
            var clientRect = new Rectangle();
            using (var brush = appearance.CreateBrush())
                dc.Graphics.FillRectangle(brush, clientRect);
            using (var pen = appearance.Border.CreatePen())
                dc.Graphics.DrawRectangle(pen, clientRect);
            for (var i = 0; i < block.SlotCount; i++)
                GetSlotView(i).Draw(dc, viewPort, block.GetSlot(i));
        }
    }
}
